package com.rayo.accesscontrollercopy.model.db.dao;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.rayo.accesscontrollercopy.model.db.DbHelp;
import com.rayo.accesscontrollercopy.model.db.bean.CalendarData;

import java.util.Date;
import java.util.List;


/**
 * Created by yanghaojie on 16/7/27.
 */
public class CalendarImpl implements CalendarDao {
    @Override
    public List<CalendarData> findAll() {
        return DbHelp.getQueryAll(CalendarData.class);
    }

    @Override
    public CalendarData findByIdAndCal(int calid, Date cal) {
        QueryBuilder qb = new QueryBuilder(CalendarData.class).whereEquals(CalendarData.CALENDAR_ID, calid)
                .whereAppendAnd().whereEquals(CalendarData.CALENDAR, cal);
        List<CalendarData> list = DbHelp.liteOrm.query(qb);
        if (null == list || list.size() ==0) return null;
        else return list.get(0);
    }

    @Override
    public List<CalendarData> findByCalId(int calid) {
        return DbHelp.liteOrm.query(new QueryBuilder<>(CalendarData.class).whereEquals(CalendarData.CALENDAR_ID, calid));
    }

    @Override
    public void addAll(List<CalendarData> calendar) {
        DbHelp.insertAll(calendar);
    }

    @Override
    public void addCalendar(CalendarData calendar) {
        DbHelp.insert(calendar);
    }

    @Override
    public void updateAll(List<CalendarData> calendar) {
        DbHelp.updateALL(calendar);
    }

    @Override
    public void updateCalendar(CalendarData calendar) {
        DbHelp.update(calendar);
    }

    @Override
    public void deleteAll() {
        DbHelp.deleteAll(CalendarData.class);
    }

    @Override
    public void deleteByCalId(int calid) {
        DbHelp.deleteWhere(CalendarData.class, CalendarData.CALENDAR_ID,
                new String[]{String.valueOf(calid)});
    }
}
